package com.sbr.tbd.wicket.panels.application;

import com.sbr.tbd.model.Application;
import com.sbr.tbd.services.AdministrationService;
import com.sbr.tbd.wicket.pages.administration.application.ListApplicationsPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EditApplicationPanel extends Panel {

	protected static final Logger LOG = LoggerFactory.getLogger(EditApplicationPanel.class);

	@SpringBean
	private AdministrationService administrationService;
	
	private class EditApplicationForm extends Form<Application> {

		public EditApplicationForm(String id) {
			super(id);
			setDefaultModel(new CompoundPropertyModel<Application>(new Application()));
			
			add(new RequiredTextField<String>("name"));
			add(new TextField<String>("description"));

		}
		
		@Override
		protected void onSubmit() {
			LOG.debug("onSubmit called");
			Application application = getModelObject();
			administrationService.addApplication(application);
			setResponsePage(ListApplicationsPage.class);
		}
		
	}
	
	public EditApplicationPanel(String id) {
		super(id);

//		final TextField<Object> alterNameText = new RequiredTextField("projectName", new PropertyModel(application, "name"));
//		final TextField<Object> alterDescriptionText = new TextField("projectDescription",new PropertyModel(application, "description"));

		add(new EditApplicationForm("addProjectForm"));
		add(new FeedbackPanel("feedbackPanel"));
	}

	public void setAdministrationService(AdministrationService administrationService) {
		this.administrationService = administrationService;
	}
	
}
